package com.yunji.cabinet.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yunji.cabinet.bo.BindCodeRemoteBO;
import com.yunji.cabinet.bo.BindCodeSelectQueryBO;
import com.yunji.cabinet.dao.IBindCodeDao;
import com.yunji.cabinet.domain.DeviceBindDO;
import com.yunji.cabinet.mapper.BindCodeMapper;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Repository;

@Repository
public class BindCodeDaoImpl extends BaseDaoImpl<BindCodeMapper, DeviceBindDO> implements IBindCodeDao {
    @Override
    public boolean delete(BindCodeRemoteBO bo) {
        return remove(new LambdaQueryWrapper<DeviceBindDO>()
                .eq(BeanUtils.isNotNull(bo.getQrCode()), DeviceBindDO::getQrCode, bo.getQrCode())
                .eq(BeanUtils.isNotNull(bo.getImei()), DeviceBindDO::getImei, bo.getImei()));
    }

    @Override
    public DeviceBindDO select(BindCodeSelectQueryBO bo) {
        return getOne(new LambdaQueryWrapper<DeviceBindDO>()
                .eq(BeanUtils.isNotNull(bo.getQrCode()), DeviceBindDO::getQrCode, bo.getQrCode())
                .eq(BeanUtils.isNotNull(bo.getImei()), DeviceBindDO::getImei, bo.getImei()));
    }

    @Override
    public boolean removeByImeiOrQrCode(BindCodeRemoteBO b) {
        LambdaQueryWrapper<DeviceBindDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DeviceBindDO::getImei, b.getImei()).or().eq(DeviceBindDO::getQrCode, b.getQrCode());
        return remove(wrapper);
    }
}
